package com.zy.controller;

import com.zy.entity.ParkingLot;
import com.zy.entity.Vehicle;
import com.zy.service.ParkingLotService;
import com.zy.service.VehicleService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
@RequestMapping("/vehicle")
public class VehicleController {
    @Value("${save.path}")
    String savePath;//图片存储路径
    private final VehicleService service;
    private final ParkingLotService parkingLotService;

    public VehicleController(VehicleService service,
                             ParkingLotService parkingLotService) {
        this.service = service;
        this.parkingLotService = parkingLotService;
    }

    @RequestMapping("/list")
    public String list(Model model) {
        List<Vehicle> entities = service.getAll();
        model.addAttribute("list", entities);//model打包成list属性完成list对象的值的传递
        return "/vehicle/list";
    }

    @RequestMapping(value = "/insert")
    public String insert(Model model) {
        model.addAttribute("vehicle", new Vehicle());
        List<ParkingLot> parkingLots = parkingLotService.getAll();
        model.addAttribute("parkingLots", parkingLots);
        return "/vehicle/edit";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@ModelAttribute("vehicle") Vehicle entity
            , @RequestParam("isActive") boolean isActive
            , @RequestParam("parkingLotId") Long parkingLotId
            , @RequestParam("file") MultipartFile file) throws Exception {

        entity.setActive(isActive);
        ParkingLot parkingLot=parkingLotService.getById(parkingLotId);
        entity.setParkingLot(parkingLot);

        //上传图片文件指定目录
        String fileName = System.currentTimeMillis() + "-" + file.getOriginalFilename();//生成文件名
        Path saveTo = Paths.get(savePath, fileName);//设置文件存储的路径
        Files.write(saveTo, file.getBytes());//完成存储文件

        //存储图片url
        entity.setPicUrl("/images/" + fileName);

        boolean result = entity.getId() != null ? service.update(entity) : service.insert(entity);
        return "redirect:/vehicle/list";
    }

    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        Vehicle entity = service.getById(id);
        model.addAttribute("vehicle", entity);
        List<ParkingLot> parkingLots = parkingLotService.getAll();
        model.addAttribute("parkingLots", parkingLots);
        return "/vehicle/edit";
    }

    @RequestMapping(value = "/detail/{id}")
    public String detail(@PathVariable("id") Long id, Model model) {
        Vehicle entity = service.getById(id);
        model.addAttribute("vehicle", entity);
        return "/vehicle/detail";
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        boolean result = service.delete(id);
        return "redirect:/vehicle/list";
    }
}
